{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "import torch\n",
    "from torch.utils.data import Dataset\n",
    "from torch.utils.data.dataloader import DataLoader\n",
    "from src.utils import set_seed\n",
    "from src.model import TEMPO, TEMPOConfig\n",
    "from src.trainer import Trainer\n",
    "from src.dataset import Dataset_ETT_minute, Dataset_ETT_hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_seed(3407)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "trainset = Dataset_ETT_hour(root_path='./data', flag='train',)\n",
    "valset = Dataset_ETT_hour(root_path='./data', flag='val')\n",
    "testset = Dataset_ETT_hour(root_path='./data', flag='test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = TEMPOConfig(\n",
    "    num_series=3,\n",
    "    input_len=trainset.seq_len,\n",
    "    pred_len=trainset.pred_len,\n",
    "    n_layer=6,\n",
    "    model_type='gpt2',\n",
    "    patch_size=16,\n",
    "    patch_stride=8,\n",
    "    lora=True,\n",
    "    lora_config={\n",
    "        'lora_r': 4,\n",
    "        'lora_alpha': 8,\n",
    "        'lora_dropout': 0.1,\n",
    "        'enable_lora': [True, True, False],\n",
    "        'fan_in_fan_out': False,\n",
    "        'merge_weights': False,\n",
    "    },\n",
    "    prompt_config={\n",
    "        'embed_dim': 768,\n",
    "        'top_k': 3,\n",
    "        'prompt_length': 3,\n",
    "        'pool_size': 30,\n",
    "    },\n",
    "    interpret=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = TEMPO.from_pretrained(\n",
    "    config=config,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num of total parameters:  46.31M\n",
      "num of trainable parameters:  3.73M\n"
     ]
    }
   ],
   "source": [
    "print(f\"num of total parameters: {model.num_params['total']/1e6: .2f}M\")\n",
    "print(f\"num of trainable parameters: {model.num_params['grad']/1e6: .2f}M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "tra = Trainer(model, use_amp=True, features=\"S\", num_workers=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "tra.train(trainset, valset, batch_size=200, max_epochs=10, lr=0.001)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
